package com.neusoft.config;

import com.neusoft.exception.AuthFailException;
import com.neusoft.exception.EmpExistException;
import com.neusoft.exception.EmpSelectException;
import com.neusoft.exception.EmpUpdateFail;
import com.neusoft.utils.ServerResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author cpx
 * @date 2024/9/28
 * @Description 自定义全局异常类
 */
@RestControllerAdvice(basePackages = "com.neusoft.controller")
@ResponseBody   // 都需要返回JSON格式的数据，统一加在类上
public class MyException extends Throwable {
    @ExceptionHandler(EmpExistException.class)
    public ServerResponse handleUserExistException(EmpExistException e) {
        return ServerResponse.error(e.getCode(),e.getMessage(),null);
    }
    @ExceptionHandler(AuthFailException.class)
    public ServerResponse  handleAuthFailException(AuthFailException e) {
        return ServerResponse .error(e.getCode(), e.getMessage(), null);
    }

    @ExceptionHandler(value = EmpSelectException.class)
    public ServerResponse empSelectException(EmpSelectException e) {
        return ServerResponse.error(e.getCode(), e.getMessage(), null);
    }

    @ExceptionHandler(value = EmpUpdateFail.class)
    public ServerResponse empUpdateFail(EmpUpdateFail e) {
        return ServerResponse.error(e.getCode(), e.getMessage(), null);
    }

    // 全局异常处理  如果有单独要进行处理的 写在全局异常处理上面
    @ExceptionHandler(value = Exception.class)
    public ServerResponse exception(Exception e){
        return ServerResponse.error(e.getMessage());
    }
}
